<template>
  <div>
    <!--搜索-->
    <div class="query-form">
      <el-form ref="form" :inline="true" :model="queryForm">
        <el-form-item label="菜单名称" prop="menuName">
          <el-input v-model="queryForm.menuName" placeholder="请输入菜单名称" />
        </el-form-item>
        <el-form-item label="菜单状态" prop="menuState">
          <el-select v-model="queryForm.menuState">
            <el-option :value="1" label="正常"></el-option>
            <el-option :value="2" label="停用"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getMenuAll">查询</el-button>
          <el-button @click="handleReset('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 搜索结束 -->
    <!-- 表格 -->
    <div class="action">
      <el-button type="primary" @click="handleAdd">新增</el-button>
    </div>
    <el-table :data="tableData" style="width: 100%;margin-bottom: 20px;" row-key="MenuId" border default-expand-all
      :tree-props="{children: 'ChildMenus', hasChildren: 'hasChildren'}">
      <el-table-column v-for="(item,index) in columns" :key="index" :prop="item.prop" :formatter="item.formatter"
        :label="item.label" :width="item.width">
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">新增</el-button>
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格结束 -->
    <!-- 模态 -->
    <!--添加模态-->
    <el-dialog title="用户新增" :visible.sync="showModal">
      <el-form ref="dialogForm" :model="menuForm" label-width="100px" :rules="rules">
        <el-form-item label="父级菜单" prop="parentId">
          <el-cascader v-model="menuForm.parentId" :options="menuList"
            :props="{ checkStrictly: true, value: '_id', label: 'menuName' }" clearable />
          <span>不选，则直接创建一级菜单</span>
        </el-form-item>
        <el-form-item label="菜单类型" prop="menuType">
          <el-radio-group v-model="menuForm.menuType">
            <el-radio :label="1">菜单</el-radio>
            <el-radio :label="2">按钮</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="菜单名称" prop="menuName">
          <el-input v-model="menuForm.menuName" placeholder="请输入菜单名称" />
        </el-form-item>
        <el-form-item label="菜单图标" prop="icon" v-show="menuForm.menuType == 1">
          <el-input v-model="menuForm.icon" placeholder="请输入岗位" />
        </el-form-item>
        <el-form-item label="路由地址" prop="path" v-show="menuForm.menuType == 1">
          <el-input v-model="menuForm.path" placeholder="请输入路由地址" />
        </el-form-item>
        <el-form-item label="权限标识" prop="menuCode" v-show="menuForm.menuType == 2">
          <el-input v-model="menuForm.menuCode" placeholder="请输入权限标识" />
        </el-form-item>
        <el-form-item label="组件路径" prop="component" v-show="menuForm.menuType == 1">
          <el-input v-model="menuForm.component" placeholder="请输入组件路径" />
        </el-form-item>
        <el-form-item label="菜单状态" prop="menuState" v-show="menuForm.menuType == 1">
          <el-radio-group v-model="menuForm.menuState">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 模态结束 -->
  </div>
</template>

<script>
import $http from '../../request/http'
export default {
  name: 'rolemenu',
  data () {
    return {
      tableData: [],
      action: '',
      showModal: false, //模态
      menuList: [], //模态下拉
      menuForm: { //模态表单
        parentId: [null],
        menuType: 1,
        menuState: 1,
      },
      rules: {
        menuName: [
          {
            required: true,
            message: "请输入菜单名称",
            trigger: "blur",
          },
          {
            min: 2,
            max: 10,
            message: "长度在2-8个字符",
            trigger: "blur",
          },
        ],
      },
      queryForm: { //查询
        menuState: 1,
      },
      columns: [
        {
          label: '菜单名称',
          prop: 'MenuName'
        },
        {
          label: '图标',
          prop: 'Icon'
        },
        {
          label: '菜单类型',
          prop: 'Type',
          formatter (row, column, value) {
            return {
              1: '菜单',
              2: '按钮'
            }[value]
          },
          width: '80px'
        },
        {
          label: '路由地址',
          prop: 'Url'
        },
        {
          label: '菜单状态',
          prop: 'Deleted',
          formatter (row, column, value) {
            return {
              'False': '启用',
              'True': '停用'
            }[value]
          },
          width: '80px'
        },
      ]
    }
  },
  methods: {
    getMenuAll () {  //获取菜单数据
      let userid = localStorage.getItem("userid");
      if (userid) {
        $http.get(`/Menu/menutree/${userid}`).then(res => {
          this.tableData = res.data.Data;
        })
      }
    },
    // 新增菜单
    handleAdd () {
      this.showModal = true;
      this.action = "add";

    },
    // 菜单操作-提交
    handleSubmit () {

    },
    // 弹框关闭
    handleClose () {
      this.showModal = false;
      this.handleReset("dialogForm");
    },
    // 表单重置
    handleReset (form) {
      this.$refs[form].resetFields();
    },
  },
  created () {
    this.getMenuAll();
  },
}
</script>

<style lang="scss" scoped>
</style>